<template>
    <form-container :title="formData.id ? '编辑产品工艺路线信息' : '新增产品工艺路线信息'" :width="550" :visible="visible"
        :destroy-on-close="true" @close="onClose">
        <a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
            <a-form-item label="编码" name="id">
                <a-input v-model:value="logic.id" placeholder="请输入编码" allow-clear />
            </a-form-item>
            <a-form-item label="产品编码" name="productionCode">
                <a-input v-model:value="logic.productionCode" allow-clear disabled />
            </a-form-item>
            <a-form-item label="工艺路线" name="routeCode">
                <a-select v-model:value="logic.routeCode" placeholder="请选择状态">
                    <a-select-option v-for="item in processData" :key="item.routeCode" :value="item.routeCode">{{
                        item.routeName }}</a-select-option>
                </a-select>
            </a-form-item>
        </a-form>
        <template #footer>
            <a-checkbox class="mr-2" v-if="!formData.id" v-model:checked="addNext">继续新增下一条</a-checkbox>
            <a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
            <a-button type="primary" :loading="submitLoading" @click="onSubmit">保存</a-button>
        </template>
    </form-container>
</template>

<script setup name="positionForm">
import { required } from '@/utils/formRules'
import { getCurrentInstance } from 'vue'
import { message } from 'ant-design-vue'
import processRoute from '@/api/production/productionProcessRouteApi'
import processApi from '@/api/mes/processinfo/processrouteApi'

// 定义emit事件
const emit = defineEmits({ successful: null })
const { proxy } = getCurrentInstance()
// 默认是关闭状态
let visible = ref(false)
const formRef = ref()
// 表单数据，也就是默认给一些数据
const formData = ref({})
const submitLoading = ref(false)
// 继续新增下一条
const addNext = ref(false)

const logic = reactive(
    {
        id: 0,
        productionCode: "",
        routeCode: ""
    }
)

// 打开抽屉
const onOpen = (record, orgId) => {
    logic.productionCode = record
    visible.value = true
    addNext.value = false
    formData.value = {
        id: formData.value.id,
        routeCode: formData.value.routeCode,
        productionCode: record
    }
    logic.id = formData.value.id
    logic.routeCode = formData.value.routeCode
    console.log(formData.value.productionCode)
    if (record) {
        formData.value = Object.assign({}, record)
    }

}
// 关闭抽屉
const onClose = () => {
    visible.value = false
}
// 默认要校验的
const formRules = {
    sName: [required('请输入学生名称')],
    age: [required('请输入年龄')],
}

// 验证并提交数据
const onSubmit = () => {
    console.log(logic)
    formRef.value.validate().then(() => {
        submitLoading.value = true
        processRoute
            .addProcessRoute(logic, logic.id)
            .then(() => {
                if (addNext.value) {
                    logic
                } else {
                    visible.value = false
                }
                emit('successful')
            })
            .finally(() => {
                submitLoading.value = false
            })
    })
}
// 调用这个函数将子组件的一些数据和方法暴露出去
defineExpose({
    onOpen
})

const processData = ref([])
const queryProcess = () => {
    //alert(1)
    processApi.bindProcess({}).then(res => {
        console.log(res)
        processData.value = res
    })
}

onMounted(() => {
    queryProcess()
})
</script>
